package com.galaxy.service.controller.system;

import com.galaxy.commom.base.annotation.WebLog;
import com.galaxy.commom.base.message.ResponseCode;
import com.galaxy.commom.base.message.ResponseResult;
import com.galaxy.service.dto.system.resource.QueryPermissionDto;
import com.galaxy.service.entity.system.Permission;
import com.galaxy.service.service.system.PermissionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权限控制类
 * @author caijinbang
 * @date 2018/10/28 00:01
 */
@RestController
@RequestMapping(value = "/system/resource")
public class PermissionController {
  @Autowired
  private PermissionService permissionService;

  @PostMapping(value = {"/list"})
  @ApiOperation(value = "获取所有的权限列表")
  @WebLog("获取所有的权限列表")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult list(){
    List<Permission> permissionList = permissionService.list();
    return ResponseResult.e(ResponseCode.OK,permissionList);
  }

  @PostMapping(value = {"/add"})
  @ApiOperation(value = "添加权限")
  @WebLog("添加权限")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult add(@RequestBody @Validated @ApiParam("权限数据") QueryPermissionDto queryPermissionDto){
    permissionService.add(queryPermissionDto);
    return ResponseResult.e(ResponseCode.OK);
  }

  @WebLog("更新权限")
  @PostMapping(value = {"/update/{id}"})
  @ApiOperation(value = "更新权限")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult update(@PathVariable("id") @ApiParam("权限ID") String id,
      @RequestBody @Validated @ApiParam("权限数据") QueryPermissionDto queryPermissionDto){
    permissionService.update(id,queryPermissionDto);
    return ResponseResult.e(ResponseCode.OK);
  }

  /**
   * 根据id删除权限
   * @param id
   * @return
   */
  @WebLog("删除权限")
  @PostMapping(value = {"/remove/{id}"})
  @ApiOperation(value = "删除权限")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult remove(@PathVariable("id") @ApiParam("权限ID") String id){
    permissionService.remove(id);
    return ResponseResult.e(ResponseCode.OK);
  }
}
